package com.h;


public class ArrayUtil {
	
	public static void main(String[] args) {
//		testArray2String();
		
//		testRemoveByIdx();
		
		
		testAddArray();
	}
	
	public static Object[] addLength(Object[] arr,int length){
		Object[] arrNew=new Object[arr.length+length];
		System.arraycopy(arr, 0, arrNew, 0, arr.length);
		return arrNew;
	}

	private static void testAddArray() {
		Object[] o1=new Object[]{1,2};
		Object[] o2=new Object[]{3,4};
		System.out.println(array2String(addArray(o1, o2)));
	}

	private static void testRemoveByIdx() {
		Object[] i=new Object[]{1,2,3};

		System.out.println(array2String(removeByIdx(i, 1)));
	}
	
	public static Object[] addArray(Object[] arr,Object[] tobeAdded){
		Object[] arrNew=new Object[arr.length+tobeAdded.length];
		System.arraycopy(arr, 0, arrNew, 0, arr.length);
		System.arraycopy(tobeAdded, 0, arrNew, arr.length, tobeAdded.length);
		return arrNew;
	}
	
	public static Object[] removeByIdx(Object[] arr,int idx){
		Object[] arrNew=new Object[arr.length-1];
		
		for (int i = idx; i < arr.length-1; i++) {
			arr[i]=arr[i+1];
		}
		
		System.arraycopy(arr, 0, arrNew, 0, arrNew.length);
		
		
		return arrNew;
	}

	private static void testArray2String() {
		String[][] arr=new String[][]{{"1","3"},{"2","4"}};
		System.out.println(array2String(arr));
	}
	
	public static String array2String(Object[] arr){
		String result="[";
		for (int i = 0; i < arr.length; i++) {
			if(i!=0){
				result+=","+arr[i];
			}else{
				result+=arr[i];
			}
		}
		result+="]";
		return result;
	}
	
	public static String array2String(Object[][] arr){
		String result="[";
		for (int i = 0; i < arr.length; i++) {
			if(i!=0){
				result+=","+array2String(arr[i]);
			}else{
				result+=array2String(arr[i]);
			}
		}
		result+="]";
		return result;
	}

}
